//
//  SN_RegisterVIPViewController.m
//  Shiyun_Newspaper
//
//  Created by Sylar on 2017/7/3.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_RegisterVIPViewController.h"

@interface SN_RegisterVIPViewController ()<IAPToolDelegate>
{
    UIScrollView *backScroll;
}

@property (nonatomic, strong) NSMutableArray *vipconfigArr;

@end

@implementation SN_RegisterVIPViewController

- (void)loadView {
    [super loadView];
    
    backScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH, SCREENHEIGH)];
    [backScroll setContentSize:CGSizeMake(SCREENWIDTH, SCREENHEIGH < 603?603:SCREENHEIGH)];
    backScroll.showsVerticalScrollIndicator = NO;
    backScroll.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:backScroll];
    
    UIImageView *backImg = [[UIImageView alloc]initWithFrame:backScroll.frame];
    
    [backImg setImage:[UIImage imageNamed:@"VIP_recharge_bg"]];
    
    [backScroll addSubview:backImg];
    
    self.navBar.navigationItem.title = @"开通VIP";
    
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    UIButton *backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [backBtn setFrame:CGRectMake(0, 0, 44, 44)];
    [backBtn.titleLabel setFont:[UIFont systemFontOfSize:17]];
    [backBtn setTitle:@"取消" forState:(UIControlStateNormal)];
    [backBtn setTitleColor:colorFromHex(@"ffffff") forState:(UIControlStateNormal)];
    [backBtn addTarget:self action:@selector(disMissVC) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    
    self.navBar.navigationItem.rightBarButtonItem = backItem;
    
    [self getVipData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)disMissVC {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)reloadCurrentData {
    
    [self.networkErrorView hideEmptyImage];
    [self getVipData];
}

- (void)getVipData {
    
    NSMutableDictionary *param = [[NSMutableDictionary alloc]init];
    [param setObject:@"api_szb_vipconfig" forKey:@"Method"];
    
    
    [Utils getWithparameters:param showHUD:YES success:^(NSInteger recode, id jsonObject) {
        
        NSDictionary *dic = (NSDictionary *)jsonObject;
        
        NSDictionary *redata = [[dic objectForKey:@"re_data"] firstObject];
        
        if (redata.count == 0) {
            return;
        }
        self.vipconfigArr = [[NSMutableArray alloc]initWithArray:[redata objectForKey:@"vipconfiglist"]];
        
        [self layoutView:redata];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self.networkErrorView showEmptyImage];
    }];
}

- (void)layoutView:(NSDictionary *)infoDic {
    
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(44, 40, SCREENWIDTH-88, SCREENWIDTH-88)];
    [backView setBackgroundColor:colorFromHex(@"fef9f9")];
    backView.layer.cornerRadius = (SCREENWIDTH-88)/2.0;
    backView.layer.borderWidth = 5.0;
    backView.layer.borderColor = [colorFromHex(@"fc756e") CGColor];
    [backScroll addSubview:backView];
    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 80, SCREENWIDTH-88, 49)];
    [label1 setFont:[UIFont boldSystemFontOfSize:49]];
    [label1 setTextColor:colorFromHex(@"ff3f3f")];
    [label1 setTextAlignment:NSTextAlignmentCenter];
    [label1 setText:@"开通VIP"];
    [backView addSubview:label1];
    
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(35, SCREENHEIGH < 603?label1.bottom+15:label1.bottom+45, SCREENWIDTH-88-70, 49)];
    [label2 setFont:[UIFont systemFontOfSize:19]];
    [label2 setTextColor:colorFromHex(@"ff3f3f")];
    [label2 setTextAlignment:NSTextAlignmentCenter];
    [label2 setText:[infoDic objectForKey:@"vipcontent"]];
    label2.numberOfLines = 0;
    [label2 sizeToFit];
    //[self setLabelSpace:label2 withValue:[infoDic objectForKey:@"vipcontent"] withFont:[UIFont systemFontOfSize:19]];
    [backView addSubview:label2];
    
    for (int i = 0; i < self.vipconfigArr.count; i++) {
        
        UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [btn setFrame:CGRectMake(51, backView.bottom+50+i*74, SCREENWIDTH - 102, 44)];
        btn.layer.cornerRadius = 22;
        [btn addTarget:self action:@selector(buyAction:) forControlEvents:(UIControlEventTouchUpInside)];
        btn.tag = i;
        [btn setBackgroundColor:colorFromHex(@"fb524a")];
        [btn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [btn.titleLabel setFont:[UIFont systemFontOfSize:20]];
        NSDictionary *dic = [self.vipconfigArr objectAtIndex:i];
        [btn setTitle:[NSString stringWithFormat:@"¥ %.2f/%@",[[dic objectForKey:@"price"] floatValue],[dic objectForKey:@"name"]] forState:(UIControlStateNormal)];
        
        [backScroll addSubview:btn];
    }
}


- (void)buyAction:(UIButton *)sender {
    
    NSDictionary *dic = [self.vipconfigArr objectAtIndex:sender.tag];
    
    if ([[dic objectForKey:@"price"] integerValue] == 0) {
        
        NSMutableDictionary *param = [[NSMutableDictionary alloc]init];
        [param setObject:@"api_szb_givevip" forKey:@"Method"];
        [param setObject:[dic objectForKey:@"product_id"] forKey:@"productid"];
        [param setObject:USERID forKey:@"userid"];
        
        [Utils getWithparameters:param showHUD:YES success:^(NSInteger recode, id jsonObject) {
            
            NSDictionary *dict = (NSDictionary *)jsonObject;
            
            
            
            [self showHint:[dict objectForKey:@"re_info"]];
            
            if (recode == 0) {
                
                NSDictionary *imformation = [SyUserInfomation acquirePersonalImformationFromMemoryWithPersonID:USERID];
                NSMutableDictionary *informationM = [NSMutableDictionary dictionaryWithDictionary:imformation];
                
                [informationM setObject:@"1" forKey:@"isvip"];
                
                [SyUserInfomation personalImformationSaveWtih:informationM];
                
                [self dismissViewControllerAnimated:YES completion:nil];
            }
            
            
        } failure:^(NSInteger recode, NSError *error) {
            [Utils alertViewWithMessage:[error localizedDescription]];
        }];
        
    }else {
        NSMutableDictionary *param = [[NSMutableDictionary alloc]init];
        [param setObject:@"api_szb_iosviporder" forKey:@"Method"];
        [param setObject:[dic objectForKey:@"product_id"] forKey:@"product_id"];
        [param setObject:USERID forKey:@"userid"];
        
        [Utils getWithparameters:param showHUD:YES success:^(NSInteger recode, id jsonObject) {
            
            NSDictionary *dict = (NSDictionary *)jsonObject;
            
            NSDictionary *re_data = [dict objectForKey:@"re_data"];
            
            NSString *orderCode = [re_data objectForKey:@"ordercode"];
            if (orderCode) {
                [[SN_IAPTool ShareTool] getProduct:[dic objectForKey:@"product_id"] order:orderCode delegate:self];
            }
            
        } failure:^(NSInteger recode, NSError *error) {
            [Utils alertViewWithMessage:[error localizedDescription]];
        }];
    }
    
    
}

#pragma mark - IAPDelegate

- (void)orderResult:(NSInteger)recode {
    
    if (recode == 0) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

-(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font {
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.alignment = NSTextAlignmentCenter;
    paraStyle.lineSpacing = 9.0; //设置行间距
    paraStyle.hyphenationFactor = 1.0;
    paraStyle.firstLineHeadIndent = 0.0;
    paraStyle.paragraphSpacingBefore = 0.0;
    paraStyle.headIndent = 0;
    paraStyle.tailIndent = 0;
    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle,NSForegroundColorAttributeName:colorFromHex(@"ff3f3f")};
    
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:str attributes:dic];
    label.attributedText = attributeStr;
}

@end
